package com.mxpio.erp.oa.controller;

import com.mxpio.erp.common.enums.OrderStatusEnums;
import com.mxpio.erp.common.oa.entity.OaBusinessTripApply;
import com.mxpio.erp.common.oa.entity.OaBusinessTripApplyLine;
import com.mxpio.erp.common.oa.service.OaBusinessTripApplyLineService;
import com.mxpio.erp.common.oa.service.OaBusinessTripApplyService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "OaBusinessTripApplyLineController", description = "出差申请单-明细")
@RestController
@RequestMapping("/erp/oa/oaBusTrip/line/")
public class OaBusinessTripApplyLineController {

    @Autowired
    private OaBusinessTripApplyLineService lineService;

    @Autowired
    private OaBusinessTripApplyService service;

    @GetMapping("list")
    @Operation(summary = "报销单-明细列表", description = "报销单-明细列表（无分页）", method = "GET")
    public Result<List<OaBusinessTripApplyLine>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<OaBusinessTripApplyLine> list = lineService.list(OaBusinessTripApplyLine.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "报销单-明细列表", description = "报销单-明细列表（分页）", method = "GET")
    public Result<Page<OaBusinessTripApplyLine>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<OaBusinessTripApplyLine> pageResult = lineService.listPage(OaBusinessTripApplyLine.class, page, criteria);
        return Result.OK(pageResult);
    }

    @GetMapping("list/{bizNo}")
    @Operation(summary = "根据ID获取报销单-明细", description = "根据ID获取报销单-明细", method = "GET")
    public Result<List<OaBusinessTripApplyLine>> getById(@PathVariable(name = "bizNo", required = true) String bizNo) {
        Criteria criteria = Criteria.create().addCriterion("bizNo", Operator.EQ, bizNo);
        List<OaBusinessTripApplyLine> list = lineService.list(OaBusinessTripApplyLine.class, criteria);
        return Result.OK(list);
    }



    @PutMapping("edit")
    @Operation(summary = "编辑", description = "编辑", method = "PUT")
    public Result<OaBusinessTripApplyLine> edit(@RequestBody OaBusinessTripApplyLine line) {
        OaBusinessTripApply byId = service.getById(OaBusinessTripApply.class,line.getBizNo());
        if (!byId.getBpmnStatus().equals(OrderStatusEnums.BpmnStatus.CREATE.getStatusCode())){
            return Result.error("单据开立状态下才可修改");
        }
        lineService.update(line);
        return Result.OK(line);
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除单子", description = "删除单子,多个id用英文逗号分隔", method = "DELETE")
    public Result<OaBusinessTripApplyLine> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        for(String key : id){
            lineService.delete(OaBusinessTripApplyLine.class, key);
        }
        return Result.OK();
    }
}
